package simpleftp.client.control;

import java.io.File;
import java.util.concurrent.Callable;

import simpleftp.client.gui.FTPClientWindow;

public final class Downloader implements Callable<Boolean> {
	private SimpleFTP client;
	private String server;
	private String port;
	private FTPClientWindow wind;
	private String user;
	private String pass;
	private File file;
	private String pathToDownload;

	public Downloader(File f, FTPClientWindow window, String server, String port,
            String user, String pass, String download) 
    {
        this.file = f;
        this.server = server;
        this.port = port;
        this.wind = window;
        this.user = user;
        this.pass = pass;
        this.pathToDownload = download;
        System.out.println("Download function : " + this.pathToDownload);
        client = new SimpleFTP();
    }
	
	private void consoleToEnd() {
		wind.console.setCaretPosition(wind.console.getDocument().getLength());
	}
	
	
	@Override
    public Boolean call() throws Exception 
    {
        System.out.println("Call download");
        Boolean result = false;
        client.connect(server, Integer.parseInt(port), user, pass);
        result = client.stordown(file, this.pathToDownload);
        if (result) 
        {
            wind.console.append("\nSuccessfully downloaded file "
                    + file.getName());
            consoleToEnd();
        } else {
            wind.console.append("\nFailed downloading file " + file.getName());
            consoleToEnd();
        }
        return result;
    }
}
